<?php

/**
 * This is the model class for table "workshift".
 *
 * The followings are the available columns in table 'workshift':
 * @property string $workshift_id
 * @property string $title
 * @property string $from_time
 * @property string $to_time
 */
class Workshift extends CActiveRecord
{

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Workshift the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'workshift';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('title', 'length', 'max' => 100),
            array('from_time, to_time, wage', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('workshift_id, title, from_time, to_time, wage', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'workshift_id' => UserModule::t('Id', array(), 'workshift'),
            'title' => UserModule::t('Title', array(), 'workshift'),
            'from_time' => UserModule::t('From time', array(), 'workshift'),
            'to_time' => UserModule::t('To time', array(), 'workshift'),
            'wage' => UserModule::t('Wage', array(), 'workshift'),
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('workshift_id', $this->workshift_id, true);
        $criteria->compare('title', $this->title, true);
        $criteria->compare('from_time', $this->from_time, true);
        $criteria->compare('to_time', $this->to_time, true);
        $criteria->compare('wage', $this->wage, true);

        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                ));
    }

    public function __set($key, $value)
    {
        if (in_array($key, array('from_time', 'to_time'))) {
            return parent::__set($key, date('H:i:s', strtotime($value)));
        }
        return parent::__set($key, $value);
    }

    public function __get($id)
    {
        if (in_array($id, array('from_time', 'to_time'))) {
            return date('g:i A', strtotime(parent::__get($id)));
        }
        return parent::__get($id);
    }

}
